/**
 * @title jwt实现策略
 * @Author webxue
 * @Date 2022/12/02
 */

import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private configService: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get("JWT_CONFIG").secret,
    });
  }

  // token校验成功,会走到这里,否则会直接走401
  async validate(payload: any) {
    return payload;
  }
}
